package com.tanhua.app.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.pojo.Question;
import com.tanhua.model.pojo.Settings;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;

    //    用户通用设置 - 读取
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢Java吗?" : question.getTxt();
        vo.setId(userId);
        vo.setStrangerQuestion(txt);
        vo.setPhone(mobile);
        Settings settings = settingsApi.findByUserId(userId);
        if(settings != null){
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    //    设置陌生人问题 - 保存
    public void saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        }else {
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    //    通知设置 - 保存
    public void saveSettings(Map map) {
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        if(settings == null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        }else {
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    //    黑名单 - 翻页列表
    public PageResult<UserInfo> blacklist(Integer page, Integer pageSize) {
        Long userId = UserHolder.getUserId();
        Page<UserInfo> pages = blackListApi.findBlackList(userId,page,pageSize);
        PageResult<UserInfo> pageResult = new PageResult<>(page, pageSize, (int) pages.getTotal(), pages.getRecords());
        return pageResult;
    }

    //    黑名单 - 移除
    public void deleteBlackList(Long uid){
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,uid);
    }
}
